package com.gzhryc.shared_device.oem.micro.api.controllers.xcx.v1.member;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.services.DeviceService;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberCommonDevice;
import com.gzhryc.shared_device.oem.code.member.services.MemberCommonDeviceService;
import com.gzhryc.shared_device.oem.micro.api.MicroConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.DefaultResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberCommonDeviceAddRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberCommonDeviceListRequest;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberCommonDeviceListResponse;
import com.gzhryc.shared_device.oem.micro.api.dto.xcx.v1.member.MemberCommonDeviceRemoveRequest;

public class MemberCommonDeviceController {

    @Route(value = { "/xcx/v1/member/common_device/list.data" })
    public MemberCommonDeviceListResponse commonDeviceList(HttpServletRequest request) {
        MemberCommonDeviceListResponse apiResp = new MemberCommonDeviceListResponse(ClientAPICodeConstants.NULL_DATA);
        MemberCommonDeviceListRequest apiReq = WebUtils.getJsonRequest(request, MemberCommonDeviceListRequest.class);
        if (apiReq != null && apiReq.getBody().getPageNo() != null && apiReq.getBody().getPageLimit() != null) {
            LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (member != null) {
                List<MemberCommonDevice> deviceList = MemberCommonDeviceService.self().findJoinByMemberId(member.getId(),
                        apiReq.getBody().getPageNo(), apiReq.getBody().getPageLimit());
                if (deviceList != null && deviceList.size() > 0) {
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setItems(new ArrayList<>());
                    List<String> deviceSnList = new ArrayList<>();
                    for (MemberCommonDevice device : deviceList) {
                        if(!device.getPlaceId().equals(device.getDevice().getPlaceId())){
                            //设备部署其它网点
                            deviceSnList.add(device.getDeviceSn());
                            continue;
                        }
                        MemberCommonDeviceListResponse.Item item = new MemberCommonDeviceListResponse.Item();
                        item.setDeviceSn(device.getDeviceSn());
                        item.setDeviceFunNames(device.getDevice().getDeviceFunNames());
                        item.setPlaceId(device.getDevice().getPlaceId());
                        item.setPlaceName(device.getDevice().getPlaceName());
                        item.setProvince(device.getDevice().getProvince());
                        item.setCity(device.getDevice().getCity());
                        item.setCityArea(device.getDevice().getCityArea());
                        apiResp.getBody().getItems().add(item);
                    }

                    if(deviceSnList.size() > 0){
                        //删除已切换部署的设备
                        MemberCommonDeviceService.self().deleteByKeys(member.getId(),deviceSnList);
                    }
                }
            }
        }else{
            apiResp.setCode(ClientAPICodeConstants.EXECUTE_ERROR, "获取收货地址失败，参数错误！");
        }
        return apiResp;
    }

    @Route(value = { "/xcx/v1/member/common_device/add.do" })
    public DefaultResponse addCommonDevice(HttpServletRequest request) {
        DefaultResponse apiResp = new DefaultResponse(ClientAPICodeConstants.EXECUTE_ERROR, "保存常用设备失败，请稍后再试！");
        MemberCommonDeviceAddRequest apiReq = WebUtils.getJsonRequest(request, MemberCommonDeviceAddRequest.class);
        if (apiReq != null) {
            LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (member != null) {
                Device device = null;
                if (StringTools.isNotBlank(apiReq.getBody().getDeviceSn())) {
                    device = DeviceService.self().getByDeviceSn(apiReq.getBody().getDeviceSn());
                } else if (apiReq.getBody().getPlaceId() != null) {
                    List<Device> deviceList = DeviceService.self().findByPlaceId(apiReq.getBody().getPlaceId());
                    if (deviceList != null && deviceList.size() > 0) {
                        device = deviceList.get(0);
                    }
                }

                if (device == null || device.getPlaceId() == null) {
                    apiResp.setMsg("设备不存在或未部署");
                    return apiResp;
                }
                
                MemberCommonDevice commonDevice = MemberCommonDeviceService.self().getByKeys(member.getId(),
                		device.getDeviceSn());
                if(commonDevice != null) {
                	 apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                }else {
	                try {
	                    commonDevice = new MemberCommonDevice();
	                    commonDevice.setMemberId(member.getId());
	                    commonDevice.setMemberName(member.getFullName());
	                    commonDevice.setDeviceSn(device.getDeviceSn());
	                    commonDevice.setPlaceId(device.getPlaceId());
	                    if (MemberCommonDeviceService.self().add(commonDevice)) {
	                        apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
	                    }
	                } catch (LogicException e) {
	                    apiResp.setMsg(e.getMessage());
	                }
                }
            }
        }
        return apiResp;
    }

    @Route(value = { "/xcx/v1/member/common_device/del.do" })
    public DefaultResponse delCommonDevice(HttpServletRequest request) {
        DefaultResponse apiResp = new DefaultResponse(ClientAPICodeConstants.EXECUTE_ERROR, "删除常用设备失败，请稍后再试！");
        MemberCommonDeviceRemoveRequest apiReq = WebUtils.getJsonRequest(request, MemberCommonDeviceRemoveRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getDeviceSn())) {
            LoginMember member = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (member != null) {
                try {
                    if (MemberCommonDeviceService.self().deleteByKeys(member.getId(), apiReq.getBody().getDeviceSn())) {
                        apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    apiResp.setMsg(e.getMessage());
                }
            }
        }
        return apiResp;
    }
}
